/**
 * Created by smp on 2017/5/17.
 */
var modelPath=ROOT_PATH+'/archives/personArchives/safetyAssessmentRewardPunishmentLedger/';
function openDetailPage(id){
    WindowManager.openLayer({
        type: 2,
        title: '编辑问题',
        area: ['1150px', '70%'],
        content:modelPath+"relationDetail.shtml?id="+id
    });
}
$("#btn_save").click(function(){
    var index=layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        var data = $('#precautionList').datagrid('getData');
        var json=JSON.stringify(data.rows);
        $('#form1').form('submit', {
            url: modelPath+'edit.shtml',

            onSubmit: function (param) {
                param.rewardPunishments=json;
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success){
                    layer.msg(result.message,{time:1000,icon:1},function () {
                        WindowManager.closeLayer({
                            returnValue: result
                        });
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

});
/*==============================================================================*/
$("#precautionList").datagrid({
    method:'post',
    url: modelPath+'relation.shtml', //请求的数据源
    queryParams: {
        id: $(":input[name=id]").val()
    },
    iconCls: 'icon-save', //图标
    //pagination: true, //显示分页
    //pageSize: 3, //页大小
    //pageList: [3, 6, 9], //页大小下拉选项此项各value是pageSize的倍数
    rownumbers:true,
    singleSelect:true,
    fit: true, //datagrid自适应宽度
    fitColumns: true, //列自适应宽度
    striped: true, //行背景交换
    nowap: true, //列内容多时自动折至第二行
    //border: false,
    toolbar:'#tb',
    columns: [[//显示的列
        {field:'relationId',width:100,checkbox:true},
        {field:'isCheckPerson',title:'是否考核个人',width:300,formatter:function(value,row,index){
            if(value==1){
                return '是';
            }else{
                return '否'
            }
        }
        },
        {field:'checkedDept',title:'被考核部门',width:200,hidden:true
        },
        {field:'checkedDeptName',title:'被考核部门',width:200,hidden:true
        },
        {field:'checkedPerson',title:'被考核个人',width:200,hidden:true
        },
        {field:'checkedPersonName',title:'被考核个人',width:200,hidden:true
        },
        {field:'checkedPersonOrUnit',title:'被考核部门/个人',width:300,formatter:function(value,row,index){
            if(row.isCheckPerson==0){
                return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+row.checkedDeptName+"</a>";
            }else{
                return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+row.checkedPersonName+"</a>";
            }
        }
        },
        {field:'happenTime',title:'发生日期',width:300},
        {field:'belongUnit',title:'所在单位',width:300,hidden:true},
        {field:'belongUnitName',title:'所在单位',width:300},
        {field:'assessment',title:'考核项目(内容）',width:300,hidden:true},
        {field:'rewardOrPunishment',title:'主要先进事迹或存在问题',width:300,hidden:true},
        {field:'opinionResult',title:'考核意见或评分结果',width:200,hidden:true},
        {field:'rewardOrPunishResult',title:'奖惩情况',width:200},
        {field:'unitOrPeople',title:'考核部门/人',width:200},
        {field:'examineDept',title:'审批部门',width:200,hidden:true},
        {field:'examineDeptName',title:'审批部门',width:200},
        {field:'remark',title:'备注',width:200,hidden:true},
        {field:'addUserId',title:'登记人',width:200,hidden:true},
        {field:'addUserName',title:'登记人',width:200},
        {field:'addDeptId',title:'登记部门',width:300,hidden:true},
        {field:'addDeptName',title:'登记部门',width:300},
        {field:'addTime',title:'登记日期',width:300}
    ]]
});
$("#btn_precaution_add").click(function(){
    WindowManager.openLayer({
        type: 2,
        title: '新增',
        area: ['70%', '80%'],
        content:modelPath+"information.shtml",
        callBack:function(result){
            $('#precautionList').datagrid('insertRow',{
                index: 0,  // 索引从0开始
                row: {
                    id:result.id,
                    isCheckPerson: result.isCheckPerson,
                    checkedDept: result.checkedDept,
                    checkedDeptName: result.checkedDeptName,
                    checkedPerson: result.checkedPerson,
                    checkedPersonName: result.checkedPersonName,
                    happenTime: result.happenTime,
                    belongUnit: result.belongUnit,
                    belongUnitName: result.belongUnitName,
                    assessment:result.assessment,
                    rewardOrPunishment: result.rewardOrPunishment,
                    opinionResult: result.opinionResult,
                    rewardOrPunishResult: result.rewardOrPunishResult,
                    unitOrPeople: result.unitOrPeople,
                    examineDept: result.examineDept,
                    examineDeptName: result.examineDeptName,
                    addUserId:result.addUserId,
                    addUserName:result.addUserName,
                    addDeptId:result.addDeptId,
                    addDeptName:result.addDeptName,
                    addTime:result.addTime,
                    remark:result.remark
                }
            });
        }
    });
});
$("#btn_precaution_delete").click(function(){
    var checkedRows=$('#precautionList').datagrid('getChecked');

    if(checkedRows.length == 0){
        layer.alert("选择需要删除的行！");
        return
    }else if (checkedRows.length > 1){
        layer.alert("只能选中一行数据！");
        return
    }
    var rowIndex = $('#precautionList').datagrid('getRowIndex', checkedRows[0]);
    $('#precautionList').datagrid('deleteRow', rowIndex);
    layer.msg("删除成功",{time:1000,icon:1});
});
$("#btn_precaution_edit").click(function(){
    var checkedRows=$('#precautionList').datagrid('getChecked');

    if(checkedRows.length == 0){
        layer.alert("选择需要删除的行！");
        return
    }else if (checkedRows.length > 1){
        layer.alert("只能选中一行数据！");
        return
    }
    var rowIndex = $('#precautionList').datagrid('getRowIndex', checkedRows[0]);
    WindowManager.openLayer({
        type: 2,
        title: '新增',
        area: ['70%', '80%'],
        content:modelPath+"informationEdit.shtml?id="+checkedRows[0].id,
        callBack:function(result){
            $('#precautionList').datagrid('updateRow',{
                index: rowIndex,  // 索引从0开始
                row: {
                    isCheckPerson: result.isCheckPerson,
                    checkedDept: result.checkedDept,
                    checkedDeptName: result.checkedDeptName,
                    checkedPerson: result.checkedPerson,
                    checkedPersonName: result.checkedPersonName,
                    happenTime: result.happenTime,
                    belongUnit: result.belongUnit,
                    belongUnitName: result.belongUnitName,
                    assessment:result.assessment,
                    rewardOrPunishment: result.rewardOrPunishment,
                    opinionResult: result.opinionResult,
                    rewardOrPunishResult: result.rewardOrPunishResult,
                    unitOrPeople: result.unitOrPeople,
                    examineDept: result.examineDept,
                    examineDeptName: result.examineDeptName,
                    addUserId:result.addUserId,
                    addUserName:result.addUserName,
                    addDeptId:result.addDeptId,
                    addDeptName:result.addDeptName,
                    addTime:result.addTime,
                    remark:result.remark
                }
            });
        }
    });
});